#include "Common.h"
//==================================================
NODE *g_BulletHead;
NODE *g_BulletEnd;
//==================================================
int g_BulletSize = 16;
int g_BulletCount;
//==================================================
char g_BulletIcon[3][3]={
	{' ','A',' '},
	{' ','M',' '},
	{'A','I','A'}};
//==================================================
VOID CALLBACK BulletTimerProc(HWND hWnd, UINT uMsg, UINT idEvent, DWORD dwTime)
{
	if(g_Pause || 0==g_Time || g_Die)
	{
		return;
	}
	
	AttackDelete();
	BulletDelete();
	Kill();
	KillMe();
	BulletMove();
	AttackMove();
	
	if(0 == g_BeeCount)
	{
		if(g_Stage<10)
		{
			g_Stage++;
			StageSet();
		}
		else
		{
			g_Pause = true;
		}
		InvalidateRect(hWnd, NULL, TRUE);
		return;
	}
	
	InvalidateRect(hWnd, NULL, false);
}
//==================================================
void BulletCreate()
{
	if(NULL == g_BulletHead)
	{
		ListCreate(&g_BulletHead, &g_BulletEnd, g_BulletIcon);
		g_BulletEnd->nX=g_PlaneEnd->nX;
		g_BulletEnd->nY=g_PlaneEnd->nY - g_PlaneSize*3;
		g_BulletEnd->nDir=DIR_UP;
	}
	else if((g_BulletEnd->nY <= (g_PlaneHead->nY - g_PlaneSize*6)) || g_BulletEnd->nY == 1280)
	{
		ListCreate(&g_BulletHead, &g_BulletEnd, g_BulletIcon);
		g_BulletEnd->nX=g_PlaneEnd->nX;
		g_BulletEnd->nY=g_PlaneEnd->nY - g_PlaneSize*3;
		g_BulletEnd->nDir=DIR_UP;
	}
}
//==================================================
void BulletDelete()
{
	NODE *pTemp = g_BulletHead;
	while(NULL != pTemp)
	{
		if(1280 == pTemp->nY || -48 > pTemp->nY)
		{
			NodeDelete(&g_BulletHead, &g_BulletEnd, pTemp);
		}
		pTemp=pTemp->pNext;
	}
	BulletSum();
}
//==================================================
void BulletClear()
{
	ListClear(&g_BulletHead, &g_BulletEnd);
}
//==================================================
void BulletMove()
{
	NODE *pTemp = g_BulletHead;
	while(NULL != pTemp)
	{
		if(DIR_NULL != pTemp->nDir)
		{
			SaveLastStatus(pTemp);
			pTemp->nY = pTemp->nY - g_BulletSize;
		}
		pTemp = pTemp->pNext;
	}
}
//==================================================
void BulletSum()
{
	g_BulletCount = 0;

	NODE *pTemp = g_BulletHead;

	while(NULL != pTemp)
	{
		g_BulletCount++;
		pTemp=pTemp->pNext;
	}
}
//==================================================
void BulletDraw(HDC hdc)
{
	NODE *pTemp = g_BulletHead;
	int nColor[3]={0x000000, 0xff0000, 0x0000ff};

	while(NULL != pTemp)
	{
		int i = 0;

		while(i<3)
		{
			RECT rl = {pTemp->nLX,pTemp->nLY+i*16,pTemp->nLX+48,pTemp->nLY+16+i*16};
			SetTextColor(hdc, 0xffffff);
			DrawText(hdc, pTemp->Text[i], 3, &rl, DT_CENTER);

			i++;
		}

		i = 0;

		while(i<3 && pTemp->nY < 1280)
		{
			RECT rt = {pTemp->nX,pTemp->nY+i*16,pTemp->nX+48,pTemp->nY+16+i*16};
			SetTextColor(hdc, nColor[i]);
			DrawText(hdc, pTemp->Text[i], 3, &rt, DT_CENTER);

			i++;
		}

		if(pTemp->nY >= 1280)
		{
			pTemp->nLX = 64;
			pTemp->nLY = 1280;
		}

		pTemp=pTemp->pNext;
	}
}
//==================================================
void Kill()
{
	NODE *pBullet = g_BulletHead;
	NODE *pBee = g_BeeHead;

	while(NULL != pBullet)
	{
		pBee = g_BeeHead;

		while(NULL != pBee)
		{
			if(pBee->nX>=0 && pBullet->nY < 416)
			{
				if( (abs(pBullet->nX - pBee->nX) < 24) && (abs(pBullet->nY - pBee->nY) < 24) )
				{
					pBullet->nDir = DIR_NULL;
					pBullet->nLY = pBullet->nY;
					pBullet->nLX = pBullet->nX;
					pBullet->nY = 1280;
					pBullet->nX = 64;

					pBee->nDir = DIR_NULL;
					pBee->nLX = pBee->nX;
					pBee->nLY = pBee->nY;
					pBee->nX = 64;
					pBee->nY = 1280;
				}
			}

			pBee=pBee->pNext;
		}

		pBullet=pBullet->pNext;
	}
}
//==================================================
void KillMe()
{
	if(g_Invincible)
	{
		return;
	}
	
	NODE *pAttack = g_AttackHead;
	NODE *pPlane = g_PlaneHead;
	
	while(NULL != pAttack)
	{
		pPlane = g_PlaneHead;
		
		while(NULL != pPlane)
		{
			if((624 - g_AttackSize * 3) <= pAttack->nY && pAttack->nY < 624)
			{
				if( (abs(pAttack->nX - pPlane->nX) < 24) && (abs(pAttack->nY - pPlane->nY) < 24) )
				{
					pAttack->nDir = DIR_NULL;
					pAttack->nLY = pAttack->nY;
					pAttack->nLX = pAttack->nX;
					pAttack->nY = 1280;
					pAttack->nX = 64;
					
					pPlane->nDir = DIR_NULL;
					pPlane->nLX = pPlane->nX;
					pPlane->nLY = pPlane->nY;
					pPlane->nX = 64;
					pPlane->nY = 1280;

					g_Die = true;
				}
			}
			
			pPlane=pPlane->pNext;
		}
		
		pAttack=pAttack->pNext;
	}
}
